import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../hb_kline_chart/hb_chart_data_util.dart';
import '../hb_kline_chart/hb_k_line_chart.dart';

class MyChartPage extends StatefulWidget {
  const MyChartPage({super.key});

  @override
  State<StatefulWidget> createState() => _MyChartPageState();
}

class _MyChartPageState extends State<MyChartPage> {
  List klineDatas = [];

  @override
  void initState() {
    // TODO: implement setState
    super.initState();

    getMockKlineData();
    print("MinePageState initState");
  }

  getMockKlineData() async {
    rootBundle
        .loadString('lib/hb_kline_chart/mock_data/k_line.json')
        .then((result) {
      List dataList = jsonDecode(result);
      List data = [];
      for (var i = 0; i < dataList.length; i++) {
        Map m = dataList[i];
        Map newMap = {
          "open": HBDataUtil.valueToNum(m["open"]).toDouble(),
          "high": HBDataUtil.valueToNum(m["high"]).toDouble(),
          "low": HBDataUtil.valueToNum(m["low"]).toDouble(),
          "close": HBDataUtil.valueToNum(m["close"]).toDouble(),
          "vol": HBDataUtil.valueToNum(m["vol"]).toDouble(),
          "date": m["date"],
        };
        data.add(newMap);
      }
      klineDatas = data;
      HBDataUtil.calculate(klineDatas);
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('绘制'),
      ),
      body: Container(
        color: Colors.white,
        child: HBKLineChart(
          key: const Key('222'),
          datas: klineDatas,
        ),
      ),
    );
  }
}
